package com.anyi.gulimall.product.service.impl;

import com.anyi.gulimall.product.dao.AttrDao;
import com.anyi.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.anyi.gulimall.product.entity.AttrEntity;
import com.anyi.gulimall.product.entity.CategoryEntity;
import com.anyi.gulimall.product.service.AttrAttrgroupRelationService;
import com.anyi.gulimall.product.service.AttrService;
import com.anyi.gulimall.product.service.CategoryService;
import com.anyi.gulimall.product.vo.AttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.anyi.gulimall.common.utils.PageUtils;
import com.anyi.gulimall.common.utils.Query;

import com.anyi.gulimall.product.dao.AttrGroupDao;
import com.anyi.gulimall.product.entity.AttrGroupEntity;
import com.anyi.gulimall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.w3c.dom.Attr;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询当前分类下的所有属性
     * @param params
     * @param categoryId
     * @return
     */
    @Override
    public PageUtils queryAttrGroup(Map<String, Object> params, Long categoryId) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        if (categoryId !=0){
            wrapper.eq(AttrGroupEntity::getCatelogId,categoryId);
        }
        String  key = (String) params.get("key");
        // 条件查询
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return  new PageUtils(page);
    }

    /**
     * 获取分组的详细信息
     * @param attrGroupId
     * @return
     */
    @Override
    public AttrGroupEntity getAttrDetail(Long attrGroupId) {
        AttrGroupEntity attrGroup = this.getById(attrGroupId);

        // 信息封装
        List<Long> paths = new ArrayList<>();

        // 递归获取分类路径
        this.getAttrPaths(attrGroup.getCatelogId(),paths);

        Collections.reverse(paths);

        attrGroup.setCatelogPath(paths.toArray(new Long[paths.size()]));

        return attrGroup;
    }

    // 根据分组id获取所有关联的属性
    @Override
    public List<AttrEntity> getAttrGroupList(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId));

        // 设置所有属性信息
        List<AttrEntity> collect = list.stream().map(item -> {
            AttrEntity attrEntity = attrService.getById(item.getAttrId());
            return attrEntity;
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 根据分组id查询没有关联的属性信息
     * @param attrGroupId
     * @return
     */
    @Override
    public PageUtils getNoAttrGroupList(Map<String,Object> params,Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId));
        AttrGroupEntity attrGroup = this.getById(attrGroupId);

        // 查出已知的分类id
        List<Long> attrIds = list.stream().map(item -> item.getAttrId()).collect(Collectors.toList());
        String  key = (String) params.get("key");
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCatelogId,attrGroup.getCatelogId());
        if(attrIds.size() !=0){
            wrapper.notIn(AttrEntity::getAttrId, attrIds);
        }
        if (StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key);
            });
        }
        // 查询出所有当前分组的属性
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params),wrapper);
        // 设置所有属性信息
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    /**
     * 根据分类id获取所有的分组信息和属性信息
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupVo> getInfoWithAttr(Long catelogId) {
        List<AttrGroupEntity> list = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        // 封装信息
        List<AttrGroupVo> attrGroupVos = list.stream().map(item -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            // 根据分类id查询出所有的属性
            List<AttrAttrgroupRelationEntity> attrgroupRelationEntities = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq(AttrAttrgroupRelationEntity::getAttrGroupId, item.getAttrGroupId()));
            List<AttrEntity> collect = attrgroupRelationEntities.stream().map( attrgroupRelationEntity-> {
                AttrEntity attrEntity = attrService.getById(attrgroupRelationEntity.getAttrId());
                return attrEntity;
            }).collect(Collectors.toList());
            attrGroupVo.setAttrs(collect);
            return attrGroupVo;
        }).collect(Collectors.toList());
        return attrGroupVos;
    }

    // 递归获取分类路径
    public List<Long> getAttrPaths(Long catelogId,List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity category = categoryService.getById(catelogId);
        if (category.getParentCid() != 0){
            this.getAttrPaths(category.getParentCid(),paths);
        }
        return paths;
    }

}